<?php

class Language {
	private $AppName;
	private $Language;
	private $Values = array();
	
	function __construct($app_name){
		$this->AppName = $app_name;
		$this->getLanguage();
		
		$LangFile = dirname(__FILE__)."/../lib/".$app_name."/lang/".$this->Language.".xml";
		
		if(!file_exists($LangFile)){
			$this->Values = false;
			return false;
		}
		
		try {
			$XML = simplexml_load_file($LangFile);
		}
		catch (Exception $e){
			throw new FMSException("Cannot load < $LangFile >", 1);
		}
		
		foreach ($XML->lang as $lang){
			$this->Values[(string)$lang["key"]] = (string)$lang;
		}
	}
	
	function getLanguage(){
		if(isset($_SESSION["fms_language"]) && is_string($_SESSION["fms_language"])){
			$this->Language = $_SESSION["fms_language"];
		}
		elseif (Core::$FMSConfig->getValue("environment", "language") != false){
			$this->Language = Core::$FMSConfig->getValue("environment", "language");
		}
		else {
			$this->Language = "en";
		}
	}
	
	function get($key){
		if(is_array($this->Values)){
			if(!empty($this->Values[$key])){
				return $this->Values[$key];
			}
		
			return false;
		}
		
		return "LANGUAGE FILE FOR < $this->AppName > NOT LOADED";
	}
}

?>